//
//  GuideViewController.m
//  CangLongWnnianli
//
//  Created by 暗流澎涌 on 2025/7/2.
//

#import "GuideViewController.h"
#import "MainTabBarController.h"
#import "HomeViewController.h"
#import <Masonry/Masonry.h>

@interface GuideViewController () <UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) UIButton *nextButton;
@property (nonatomic, strong) UIButton *startButton;
@property (nonatomic, assign) NSInteger currentPage;

@end

@implementation GuideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI {
    // 设置渐变背景
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = self.view.bounds;
    gradientLayer.colors = @[
        (id)[UIColor colorWithRed:0.773 green:0.188 blue:0.188 alpha:1.0].CGColor,
        (id)[UIColor colorWithRed:0.839 green:0.620 blue:0.180 alpha:1.0].CGColor
    ];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1, 1);
    [self.view.layer insertSublayer:gradientLayer atIndex:0];
    
    // 滚动视图
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.delegate = self;
    [self.view addSubview:self.scrollView];
    
    // 内容视图
    self.contentView = [[UIView alloc] init];
    [self.scrollView addSubview:self.contentView];
    
    // 创建三个引导页面
    [self createGuidePages];
    
    // 页面指示器
    self.pageControl = [[UIPageControl alloc] init];
    self.pageControl.numberOfPages = 3;
    self.pageControl.currentPage = 0;
    self.pageControl.pageIndicatorTintColor = [UIColor colorWithWhite:1.0 alpha:0.5];
    self.pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    [self.view addSubview:self.pageControl];
    
    // 继续按钮
    self.nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.nextButton setTitle:@"继续" forState:UIControlStateNormal];
    [self.nextButton setTitleColor:[UIColor colorWithRed:0.773 green:0.188 blue:0.188 alpha:1.0] forState:UIControlStateNormal];
    self.nextButton.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    self.nextButton.backgroundColor = [UIColor whiteColor];
    self.nextButton.layer.cornerRadius = 8;
    [self.nextButton addTarget:self action:@selector(nextButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.nextButton];
    
    // 开始使用按钮（最后一页显示）
    self.startButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.startButton setTitle:@"开始使用" forState:UIControlStateNormal];
    [self.startButton setTitleColor:[UIColor colorWithRed:0.773 green:0.188 blue:0.188 alpha:1.0] forState:UIControlStateNormal];
    self.startButton.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    self.startButton.backgroundColor = [UIColor whiteColor];
    self.startButton.layer.cornerRadius = 8;
    self.startButton.alpha = 0;
    [self.startButton addTarget:self action:@selector(startButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.startButton];
    
    [self setupConstraints];
}

- (void)createGuidePages {
    NSArray *titles = @[@"生日记录器", @"万年历", @"静心木鱼"];
    NSArray *subtitles = @[
        @"记录亲朋好友的农历生日，不错过每一个重要时刻，传递温暖与祝福",
        @"敲击木鱼，静心冥想，在繁忙的生活中找到内心的宁静与平和",
        @"传统黄历与现代科技完美结合，随时掌握吉凶宜忌，为您的每一天提供智慧指引",
    ];
    
    CGFloat screenWidth = UIScreen.mainScreen.bounds.size.width;
    NSArray * iamgeArr = @[@"welOne",@"welTwo",@"welThree"];
    for (int i = 0; i < 3; i++) {
        UIView *pageView = [[UIView alloc] init];
        [self.contentView addSubview:pageView];
        
        // 图片容器
        UIView *imageContainer = [[UIView alloc] init];
        imageContainer.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.2];
        imageContainer.layer.cornerRadius = 16;
        [pageView addSubview:imageContainer];
        
        // 图片
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.layer.cornerRadius = 16;
        imageView.clipsToBounds = YES;
        imageView.backgroundColor = [UIColor colorWithRed:0.773 green:0.188 blue:0.188 alpha:0.2];
        imageView.image = [UIImage imageNamed:iamgeArr[i]];
        [imageContainer addSubview:imageView];
        
        // 标题
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = titles[i];
        titleLabel.font = [UIFont boldSystemFontOfSize:24];
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.numberOfLines = 0;
        [pageView addSubview:titleLabel];
        
        // 副标题
        UILabel *subtitleLabel = [[UILabel alloc] init];
        subtitleLabel.text = subtitles[i];
        subtitleLabel.font = [UIFont systemFontOfSize:16];
        subtitleLabel.textColor = [UIColor colorWithRed:0.996 green:0.988 blue:0.749 alpha:1.0];
        subtitleLabel.textAlignment = NSTextAlignmentCenter;
        subtitleLabel.numberOfLines = 0;
        [pageView addSubview:subtitleLabel];
        
        // 设置约束
        [pageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(self.contentView);
            make.width.mas_equalTo(screenWidth);
            make.left.equalTo(self.contentView).offset(i * screenWidth);
        }];
        
        [imageContainer mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(pageView).offset(120);
            make.centerX.equalTo(pageView);
            make.width.height.mas_equalTo(256);
        }];
        
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(imageContainer);
        }];
        
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(imageContainer.mas_bottom).offset(32);
            make.left.right.equalTo(pageView).inset(32);
        }];
        
        [subtitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(titleLabel.mas_bottom).offset(16);
            make.left.right.equalTo(pageView).inset(32);
        }];
    }
    
    // 设置内容视图的宽度
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.height.equalTo(self.scrollView);
        make.width.mas_equalTo(screenWidth * 3);
    }];
}

- (void)setupConstraints {
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-50);
    }];
    
    [self.nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.pageControl.mas_top).offset(-32);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(48);
    }];
    
    [self.startButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.pageControl.mas_top).offset(-32);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(48);
    }];
}

- (void)nextButtonTapped {
    if (self.currentPage < 2) {
        [self.scrollView setContentOffset:CGPointMake((self.currentPage + 1) * UIScreen.mainScreen.bounds.size.width, 0) animated:YES];
    }
}

- (void)startButtonTapped {
    MainTabBarController *mainTabBar = [[MainTabBarController alloc] init];
    mainTabBar.modalPresentationStyle = UIModalPresentationFullScreen;
    mainTabBar.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:mainTabBar animated:YES completion:nil];
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat pageWidth = scrollView.frame.size.width;
    NSInteger page = (NSInteger)((scrollView.contentOffset.x + pageWidth / 2) / pageWidth);
    
    if (page != self.currentPage) {
        self.currentPage = page;
        self.pageControl.currentPage = page;
        
        // 更新按钮显示
        if (page == 2) {
            [UIView animateWithDuration:0.3 animations:^{
                self.nextButton.alpha = 0;
                self.startButton.alpha = 1;
            }];
        } else {
            [UIView animateWithDuration:0.3 animations:^{
                self.nextButton.alpha = 1;
                self.startButton.alpha = 0;
            }];
        }
    }
}

@end 
